﻿using System;
using System.Collections.Generic;
using System.Drawing;
using System.IO;
//using System.Windows.Media.Imaging;
using System.Net;
using System.Collections;

namespace EpubLibrary.Image
{
    /// <summary>
    /// Taken from http://stackoverflow.com/questions/111345/getting-image-dimensions-without-reading-the-entire-file/111349
    /// Minor improvements including supporting unsigned 16-bit integers when decoding Jfif and added logic
    /// to load the image using new Bitmap if reading the headers fails
    /// </summary>
    public static class ImageHeader
    {
        private static Size GetDimensions(BinaryReader binaryReader)
        {
            byte[] bitmap = new byte[] { 0x42, 0x4D };
            byte[] gif = new byte[] { 0x47, 0x49, 0x46, 0x38, 0x37, 0x61 };
            byte[] png = new byte[] {  0x89, 0x50, 0x4E, 0x47, 0x0D, 0x0A, 0x1A, 0x0A };
            byte[] jpeg = new byte[] { 0xff, 0xd8 };

           
            int maxMagicBytesLength = png.Length;
            byte[] magicBytes = new byte[maxMagicBytesLength];
            for (int i = 0; i < maxMagicBytesLength; i++)
            {
                magicBytes[i] = binaryReader.ReadByte();
                if (StartsWith(magicBytes, bitmap))
                    return DecodeBitmap(binaryReader);
                else if (StartsWith(magicBytes, gif))
                    return DecodeGif(binaryReader);
                else if(StartsWith(magicBytes, png))
                    return DecodePng(binaryReader);
                else if(StartsWith(magicBytes, jpeg))
                    return DecodeJfif(binaryReader);                                    
            }
            throw new ArgumentException("Can't recognize image format", "binaryReader");
            return new Size(0,0);
        }

        /// <summary>        
        /// Gets the dimensions of an image.        
        /// </summary>        
        /// <param name="path">The path of the image to get the dimensions of.</param>        
        /// <returns>The dimensions of the specified image.</returns>        
        /// <exception cref="ArgumentException">The image was of an unrecognised format.</exception>        
        public static Size GetDimensions(string path)
        {
            try
            {
                HttpWebRequest myRequest = (HttpWebRequest)WebRequest.Create(path);
                //// set the method to GET to get the image
                myRequest.Method = "GET";
                //// get the response from the webpage
                HttpWebResponse imageStream = (HttpWebResponse)myRequest.GetResponse();
                try
                {
                    using (BinaryReader binaryReader = new BinaryReader(imageStream.GetResponseStream()))//File.OpenRead(path)))
                    {
                        return GetDimensions(binaryReader);
                    }
                }
                catch (ArgumentException)
                {
                    using (Bitmap b = new Bitmap(imageStream.GetResponseStream()))
                    {
                        return b.Size;
                    }
                }
            }
            catch (Exception ex)
            {
                return new Size(0, 0);
            }
        }

        private static bool StartsWith(byte[] thisBytes, byte[] thatBytes)
        {
            for (int i = 0; i < thatBytes.Length; i += 1)
            {
                if (thisBytes[i] != thatBytes[i])
                {
                    return false;
                }
            }

            return true;
        }

        private static short ReadLittleEndianInt16(BinaryReader binaryReader)
        {
            byte[] bytes = new byte[sizeof(short)];

            for (int i = 0; i < sizeof(short); i += 1)
            {
                bytes[sizeof(short) - 1 - i] = binaryReader.ReadByte();
            }
            return BitConverter.ToInt16(bytes, 0);
        }

        private static ushort ReadLittleEndianUInt16(BinaryReader binaryReader)
        {
            byte[] bytes = new byte[sizeof(ushort)];

            for (int i = 0; i < sizeof(ushort); i += 1)
            {
                bytes[sizeof(ushort) - 1 - i] = binaryReader.ReadByte();
            }
            return BitConverter.ToUInt16(bytes, 0);
        }

        private static int ReadLittleEndianInt32(BinaryReader binaryReader)
        {
            byte[] bytes = new byte[sizeof(int)];
            for (int i = 0; i < sizeof(int); i += 1)
            {
                bytes[sizeof(int) - 1 - i] = binaryReader.ReadByte();
            }
            return BitConverter.ToInt32(bytes, 0);
        }

        private static Size DecodeBitmap(BinaryReader binaryReader)
        {
            binaryReader.ReadBytes(16);
            int width = binaryReader.ReadInt32();
            int height = binaryReader.ReadInt32();
            return new Size(width, height);
        }

        private static Size DecodeGif(BinaryReader binaryReader)
        {
            int width = binaryReader.ReadInt16();
            int height = binaryReader.ReadInt16();
            return new Size(width, height);
        }

        private static Size DecodePng(BinaryReader binaryReader)
        {
            binaryReader.ReadBytes(8);
            int width = ReadLittleEndianInt32(binaryReader);
            int height = ReadLittleEndianInt32(binaryReader);
            return new Size(width, height);
        }

        private static Size DecodeJfif(BinaryReader binaryReader)
        {
            while (binaryReader.ReadByte() == 0xff)
            {
                byte marker = binaryReader.ReadByte();
                short chunkLength = ReadLittleEndianInt16(binaryReader);
                if (marker == 0xc0)
                {
                    binaryReader.ReadByte();
                    int height = ReadLittleEndianInt16(binaryReader);
                    int width = ReadLittleEndianInt16(binaryReader);
                    return new Size(width, height);
                }

                if (chunkLength < 0)
                {
                    ushort uchunkLength = (ushort)chunkLength;
                    binaryReader.ReadBytes(uchunkLength - 2);
                }
                else
                {
                    binaryReader.ReadBytes(chunkLength - 2);
                }
            }

            throw new ArgumentException("Can't read binary");
        }
    }
}
